package org.jc.subject.domain.handler.subject;

import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import org.jc.subject.common.enums.IsDeletedFlagEnum;
import org.jc.subject.common.enums.SubjectInfoTypeEnum;
import org.jc.subject.domain.entity.SubjectInfoBO;
import org.jc.subject.domain.entity.SubjectOptionBO;
import org.jc.subject.infra.basic.entity.SubjectMultiple;
import org.jc.subject.infra.basic.entity.SubjectRadio;
import org.jc.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 功能描述
 *
 * @author wanghongqian
 * @date 2024/06/24 23:01
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectMultipleService subjectMultipleService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        List<SubjectMultiple> subjectMultipleList = new LinkedList<>();

        Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoBO.getOptionList()),"选项不能为空");

        subjectInfoBO.getOptionList().forEach(option -> {
            SubjectMultiple subjectMultiple = new SubjectMultiple();
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setOptionType(option.getOptionType());
            subjectMultiple.setOptionContent(option.getOptionContent());
            subjectMultiple.setIsCorrect(option.getIsCorrect());
            subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            subjectMultipleList.add(subjectMultiple);
        });
        subjectMultipleService.insertBatch(subjectMultipleList);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectMultiple condition = new SubjectMultiple();
        condition.setSubjectId(subjectId);
        List<SubjectMultiple> subjectMultiples = subjectMultipleService.queryByCondition(condition);
        List<SubjectInfoBO.SubjectAnswerBO> answerBOList = new ArrayList<>();
        subjectMultiples.forEach(o -> {
            SubjectInfoBO.SubjectAnswerBO bo = new SubjectInfoBO.SubjectAnswerBO();
            bo.setOptionType(o.getOptionType());
            bo.setOptionContent(o.getOptionContent());
            bo.setIsCorrect(o.getIsCorrect());
            answerBOList.add(bo);
        });

        SubjectOptionBO bo = new SubjectOptionBO();
        bo.setOptionList(answerBOList);
        return bo;
    }
}
